
<template>
  <div class="mod-shopGoodsType">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.goodsTypeName" placeholder="商品分类名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isAuth('shopgoods:shopGoodsType:list')" @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('shopgoods:shopGoodsType:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button v-if="isAuth('shopgoods:shopGoodsType:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <!-- @selection-change="selectionChangeHandle"
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column
        v-if="show"
        prop="goodsTypeId"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <table-tree-column
        prop="goodsTypeName"
        header-align="center"
        treeKey="goodsTypeId"
        parentKey="parentGoodsTypeId"
        align="left"
        label="商品分类名">
      </table-tree-column>

      <el-table-column
        prop="wapDescImg"
        header-align="center"
        align="center"
        label="wap分类图片">
        <template slot-scope="scope">
          <img :src="scope.row.wapDescImg" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column
        prop="pcDescImg"
        header-align="center"
        align="center"
        label="pc分类图片">
        <template slot-scope="scope">
          <img :src="scope.row.pcDescImg" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column
        prop="chooseTypeState"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.goodsTypeState === 0" size="small" type="success"
          @click.native="updateGoodsTypeState(scope.row.goodsTypeId,1)" class="point1">启用</el-tag>
          <el-tag v-if="scope.row.goodsTypeState === 1" size="small" type="danger"
          @click.native="updateGoodsTypeState(scope.row.goodsTypeId,0)" class="point1">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="操作员">
      </el-table-column>
      <el-table-column
        prop="operdate"
        header-align="center"
        align="center"
        label="操作时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('shopgoods:shopGoodsType:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.goodsTypeId)">修改</el-button>
          <!-- <el-button v-if="isAuth('shopgoods:shopGoodsType:delete')" type="text" size="small" @click="deleteHandle(scope.row.goodsTypeId)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import TableTreeColumn from '@/components/table-tree-column'
  import AddOrUpdate from './shopGoodsType-add-or-update'
  import { treeDataTranslate } from '@/utils'
  // import Sortable from 'sortablejs'
  export default {
    data () {
      return {
        dataForm: {
          goodsTypeName: ''
        },
        goodsTypeIds: [],
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        show: false
      }
    },
    components: {
      TableTreeColumn,
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/pig-center-biz/shopgoods/shopGoodsType/list'),
          method: 'get',
          params: this.$http.adornParams({
            'name': this.dataForm.goodsTypeName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // console.log(data)
            // this.dataList = data.list
            this.dataList = treeDataTranslate(data.list, 'goodsTypeId', 'parentGoodsTypeId')
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var goodsTypeIds = id ? [id] : this.dataListSelections.map(item => {
          return item.goodsTypeId
        })
        this.$confirm(`确定对[id=${goodsTypeIds.join(',')}]进行[${goodsTypeIds ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/pig-center-biz/shopgoods/shopGoodsType/delete'),
            method: 'post',
            data: this.$http.adornData(goodsTypeIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 修改状态
      updateGoodsTypeState (id, state) {
        this.$http({
          url: this.$http.adornUrl('/pig-center-biz/shopgoods/shopGoodsType/setState'),
          method: 'post',
          params: this.$http.adornParams({
            'goodsTypeId': id,
            'goodsTypeState': state
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      loadImage (path) {
        return this.$http.adornUrl(`/pig-file/center/file/openImg?url=` + path)
      }
    }
  }
</script>
<style>
.point1{
  cursor:pointer
}
</style>
